package cn.tedu.blog.server.controller;


import cn.tedu.blog.server.pojo.dto.LinkAddNewDTO;
import cn.tedu.blog.server.pojo.dto.LinkEditDTO;
import cn.tedu.blog.server.pojo.vo.LinkDetailVO;
import cn.tedu.blog.server.pojo.vo.LinkListItemVO;
import cn.tedu.blog.server.service.ILinkService;
import cn.tedu.blog.server.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "友链模块")
@RestController
@Slf4j
@RequestMapping("/links")
public class LinkController {
    @Autowired
    private ILinkService linkService;
    
    
    @ApiOperation("添加友链")
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody LinkAddNewDTO linkAddNewDTO) {
        log.debug("接收到添加友链的请求");
        linkService.addNew(linkAddNewDTO);
        return JsonResult.ok();
    }
    
    @ApiOperation("删除友链")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable long id) {
        log.debug("接收到根据id删除友链的请求");
        linkService.deleteById(id);
        return JsonResult.ok();
    }
    
    @ApiOperation("批量删除友链")
    @PostMapping("/deleteByIds")
    public JsonResult deleteByIds(long... ids) {
        log.debug("接收到批量删除友链的请求");
        int rows = linkService.deleteByIds(ids);
        String message = "成功删除" + rows + "条友链";
        return JsonResult.ok(message);
    }
    
    @ApiOperation("修改友链")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable long id, @RequestBody LinkEditDTO linkEditDTO) {
        log.debug("接收到修改友链的请求");
        linkService.updateById(id, linkEditDTO);
        return JsonResult.ok();
    }
    
    @ApiOperation("查询单个友链")
    @GetMapping("/get")
    public JsonResult getById(String name) {
        log.debug("接收到查询单个友链的请求");
        LinkDetailVO linkDetailVO = linkService.getByName(name);
        return JsonResult.ok(linkDetailVO);
    }
    
    @ApiOperation("友链列表")
    @GetMapping("")
    public JsonResult list() {
        log.debug("接收到查询友链列表的请求");
        List<LinkListItemVO> list = linkService.list();
        return JsonResult.ok(list);
    }
    
}
